package com.dataimport.service.impl;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.StudentScholarshipInfo;
import com.dataimport.entity.sourceentity.TeacherAwardsInfo;
import com.dataimport.entity.targetentity.Awards;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.mapper.InsertSchoolCalendarMapper;
import com.dataimport.mapper.StudentScholarshipInfoMapper;
import com.dataimport.service.AwardsService;
import com.dataimport.service.TeacherAwardsInfoService;
import com.dataimport.mapper.TeacherAwardsInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TeacherAwardsInfoServiceImpl extends ServiceImpl<TeacherAwardsInfoMapper, TeacherAwardsInfo> implements TeacherAwardsInfoService{

    private Cache<String, Long> fifoCache;

    @Autowired
    private TeacherAwardsInfoMapper teacherAwardsInfoMapper;

    @Autowired
    private InsertSchoolCalendarMapper insertSchoolCalendarMapper;

    @Autowired
    private AwardsService awardsService;

    public List<Awards> selectData(Long currentPage){
        //开启数据库2
        DynamicDataSourceContextHolder.push("mysql2");
        //查询全部的院系即单位
        Page<TeacherAwardsInfo> page = new Page<>(currentPage,1000);
        Page<TeacherAwardsInfo> studentScholarshipInfoPage = teacherAwardsInfoMapper.selectPage(page, null);
        List<TeacherAwardsInfo> records = studentScholarshipInfoPage.getRecords();
        List<Awards> collect = records.stream().filter(new Predicate<TeacherAwardsInfo>() {
            @Override
            public boolean test(TeacherAwardsInfo studentScholarshipInfo) {
                return true;
            }
        }).map(new Function<TeacherAwardsInfo, Awards>() {
            @Override
            public Awards apply(TeacherAwardsInfo teacherAwardsInfo) {
                Awards awards = new Awards();
                awards.setUserCode(teacherAwardsInfo.getGh());
                awards.setAwardsName(teacherAwardsInfo.getJlmc());
                DateTime date = DateUtil.parse(teacherAwardsInfo.getHjrq());
                String[] s = date.toString().split("-");
                Integer year = Integer.parseInt(s[0]) + 1;
                //得到月份
                int month = date.month() + 1;
                String semeter = null;
                if (3<=month && month<=9){
                    semeter = "1";
                }else if (month<3 && month>=1 || month>9 && month<=12){
                    semeter = "0";
                }
                String key = s[0] + "-" + year + "-" + semeter;
                awards.setCalendarId(fifoCache.get(key));
                return awards;
            }
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void dealWithTeacherAwardsInfo() {
        //缓存空间
        List<SchoolCalendar> schoolCalendarList = insertSchoolCalendarMapper.selectList(null);
        fifoCache = CacheUtil.newFIFOCache(schoolCalendarList.size());
        for (SchoolCalendar schoolCalendar : schoolCalendarList) {
            String key = schoolCalendar.getSchoolYear().trim().replace(" ", "") + "-" + schoolCalendar.getSemester();
            fifoCache.put(key, schoolCalendar.getId());
        }

        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<Awards> awardsList = selectData(currentPage);
            if (ObjectUtils.isEmpty(awardsList)) {
                break;
            }
            insert(awardsList);
            System.out.println("插入成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(List<Awards> awardsList){
        DynamicDataSourceContextHolder.push("mysql1");
        this.awardsService.saveBatch(awardsList,awardsList.size());
        DynamicDataSourceContextHolder.poll();
    }
}




